<template>
  <div class="box">
    <div class="f1">
      <ul>
        <li>
          <div class="icon">
            <img class="icon1" src="@/assets/PC/金融服务/组30.png" alt="" />
            <div class="icon2"></div>
          </div>
          <span>填写用信信息</span>
        </li>
        <li>
          <div class="icon">
            <img class="icon1" src="@/assets/PC/金融服务/组32.png" alt="" />
          </div>
          <span>业务提交完成</span>
        </li>
      </ul>
    </div>

    <div class="f2">
      <ul class="ul">
        <li style="position: relative">
          <el-form :model="form" label-width="150px" :rules="rules">
            <el-form-item label="预计使用保函日期" prop="b">
              <el-date-picker
                v-model="form.b"
                type="date"
                placeholder="请选择使用保函日期"
                value-format="YYYY-MM-DD"
                style="width: 240px"
                size="large"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="预计使用保函额度" prop="a">
              <el-input
                placeholder="请输入使用保函额度"
                v-model="addform.a"
                @focus="
                  () => {
                    this.addform.a = remoney(this.addform.a);
                  }
                "
                @input="limitInput($event, 'a')"
                @blur="a"
              ></el-input>
              <p
                style="
                  position: absolute;
                  right: 15px;
                  top: 5px;
                  z-index: 99;
                  font-size: 16px;
                "
              >
                元
              </p>
            </el-form-item>
            <el-form-item label="预计使用保函期限" prop="c">
              <el-input
                placeholder="请输入使用保函期限"
                v-model="form.c"
              ></el-input>
              <p
                style="
                  position: absolute;
                  right: 15px;
                  top: 5px;
                  z-index: 99;
                  font-size: 16px;
                "
              >
                月
              </p>
            </el-form-item>
          </el-form>
        </li>
      </ul>
      <div class="submit">
        <button
          class="btn btn3 active"
          @click="goNextFlow()"
          v-bind:class="{ actives: submitMessage == '提交中' }"
        >
          {{ submitMessage }}
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { myPostFetch } from "@/utils";
import { ElMessage } from "element-plus";
export default {
  data() {
    return {
      submitMessage: "提交",
      isLock: false,
      procedureId: "", // 从路由地址中获取产品ID,保存到状态变量中
      applyid: "",
      gseb: [],
      form: {
        a: "",
        b: "",
        c: "",
      },
      // 用户填写资料表格
      addform: {
        a: "",
      },
      // 表单验证
      rules: {
        a: [{ required: true, message: " ", trigger: "blur" }],
        b: [{ required: true, message: " ", trigger: "blur" }],
        c: [{ required: true, message: " ", trigger: "change" }],
      },
    };
  },

  // 生命周期方法
  mounted() {
    // 从路由地址中获取产品ID,保存到状态变量中
    this.procedureId = localStorage.getItem("productcodeId");
    this.applyid = localStorage.getItem("zhapplyid");
  },
  methods: {
    // 产品申请第1步
    async goNextFlow() {
      if (this.submitMessage == "提交中") return;
      let { a, b, c } = this.form;
      if (a == "" || b == "" || c == "") return ElMessage.error("请输入内容");
      this.submitMessage = "提交中";
      let url = "/api/hubeibusienss/EBBHputout";
      let params = {
        applyamount: a,
        applytermmonth: c,
        estimateddate: b,
        productid: this.procedureId,
        applyid: this.applyid,
      };
      let res = await myPostFetch(url, params);
      if (res.code == 101) {
        ElMessage.error("额度不足");
        this.submitMessage = "提交";
        return;
      }
      if (res.code == 500) {
        ElMessage.error("服务器错误");
        this.submitMessage = "提交";
        return;
      }
      this.$router.push("/yxSubmit");
      localStorage.removeItem("productcode");
      localStorage.removeItem("productcodeId");
      localStorage.removeItem("productname");
      localStorage.removeItem("zhapplyid");
      this.submitMessage = "提交";
    },

    // 表单
    a(e) {
      this.form.a = this.addform.a;
      //获取输入的数据，若不写这行，数据可能输不进去
      this.addform.a = e.target.value;
      //将用户输入的数据，自动转换为带千分符的
      this.addform.a = this.fmoney(this.addform.a);
      let value = Number(this.form.a);
      if (this.form.a == "") return;
      this.form.a = value.toFixed(2);
      if (isNaN(this.form.a)) {
        this.form.a = "";
      }
    },
    // 表单格式转换
    // 票面金额
    limitInput(value, name) {
      this.addform[name] =
        ("" + value) // 第一步：转成字符串
          .replace(/[^\d^\.]+/g, "") // 第二步：把不是数字，不是小数点的过滤掉
          .replace(/^0+(\d)/, "$1") // 第三步：第一位0开头，0后面为数字，则过滤掉，取后面的数字
          .replace(/^\./, "0.") // 第四步：如果输入的第一位为小数点，则替换成 0. 实现自动补全
          .match(/^\d*(\.?\d{0,2})/g)[0] || "";
    },
    //千分位格式化：10000转为10,000.00
    fmoney(s, n) {
      if (s == "" || s == null || (s == undefined) | isNaN(Number(s))) {
        return s;
      }
      s = Number(s) ? Number(s) : s;
      n = (n || n == 0) && n > -1 && n <= 20 ? n : 2;
      s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
      var l = s.split(".")[0].split("").reverse();
      var r = s.split(".")[1];
      var t = "";
      for (var i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && i + 1 != l.length ? "," : "");
      }
      if (n > 0) {
        r = "." + r;
      } else if (n == 0) {
        r = "";
      }
      let returnT = t.split("").reverse().join("") + r;
      //负数处理
      if (returnT[0] == "-" && returnT[1] == ",") {
        returnT = "-" + returnT.substring(2);
      }
      return returnT;
    },
    //千分位反格式化:10,000.00转为10000.00
    remoney(s) {
      let str = "";
      if (s) {
        str = String(s).replace(/,/g, "");
      }
      if (s && (s + "").indexOf(".") > -1 && Number(str)) {
        return String(s).replace(/[^\d\.-]/g, "");
      } else if (s && Number(str)) {
        return str;
      } else {
        return s;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
  overflow: hidden;
  .f1 {
    margin-top: 20px;
    ul {
      display: flex;
      align-items: center;
      li {
        display: flex;
        flex-direction: column;
        height: 40px;
        .icon {
          display: flex;
          align-items: center;
          .icon1 {
            width: 40px;
          }
          .icon2 {
            width: 360px;
            height: 3px;
            background-color: #dbdbdb;
            margin: 0 8px;
          }
        }
        span {
          font-size: 14px;
          font-family: Source Han Sans SC;
          font-weight: 400;
          color: #999999;
          margin-left: -19px;
          margin-top: 3px;
        }
      }
    }
  }
  .f2 {
    position: relative;
    overflow: auto;
    margin-top: 10%;
    width: 100%;
    height: 300px;
    ul {
      display: flex;
      flex-direction: column;
      justify-content: center;
      li {
        width: 95%;
        margin-bottom: 18px;
        height: 50px;
        display: flex;
        justify-content: center;
        .message {
          margin-top: -11px;
          font-size: 12px;
          font-family: Source Han Sans SC;
          font-weight: 300;
          color: #ef2d46;
        }
      }
    }
    // 按钮组
    .submit {
      position: absolute;
      bottom: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 50px;
      margin: 5px 0 5px 0;
      cursor: pointer;
      .btn {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 180px;
        height: 34px;
        background: #0a7be0;
        border-radius: 4px;
        font-size: 16px;
        font-family: Source Han Sans SC;
        font-weight: 300;
        color: #ffffff;
        margin-right: 30px;
      }
      .btn1 {
        background-color: #bfbfbf;
      }
      // 提交按钮
      .btn3 {
        background-color: #ccc;
        &.active {
          background-color: #0a7be0;
        }
        &.actives {
          background-color: rgba(10, 123, 224, 0.6);
        }
      }
    }
  }
}
/* 输入框样式  */
::v-deep(.el-input__inner) {
  box-sizing: border-box;
  width: 280px;
  height: 39px;
  font-size: 15px;
  border: 1px solid #e0e0e0;
  border-radius: 0px;
  padding: 10px 10px;
  color: #606266;
  outline: 0;
  line-height: 35px !important;
}

::v-deep(.el-input__prefix) {
  position: absolute;
  right: 5px;
}
/* 时间选择器 */
::v-deep(.el-input--large .el-input__wrapper) {
  padding: 1px 10px 1px 11px;
}
</style>
